package org.uim.cricket.storage.raise;

import org.uim.cricket.db.DBThread;
import org.uim.cricket.entities.FullPlayer;
import org.uim.cricket.entities.Official;
import org.uim.cricket.entities.match.CricketMatch;
import org.uim.cricket.entities.parser.filters.impl.DefaultPlayerDetailFilter;
import org.uim.cricket.entities.parser.impl.HTMLPlayerPageParser;
import org.uim.cricket.storage.Dlogger;
import org.uim.cricket.storage.backup.BackupFetchedPlayer;
import org.uim.cricket.storage.queries.PlayerQueryGenerator;
import org.uim.cricket.storage.raise.fetch.OfficialArgumentBuilder;
import org.uim.cricket.storage.raise.fetch.OrderedFetchList;
import org.uim.cricket.storage.raise.fetch.ResourceFetchable;

public class InsertNewOfficial extends InsertNewEntity {
	
	private Official official = null;
	private CricketMatch match = null;
	
	public InsertNewOfficial(OrderedFetchList rfetcher, CricketMatch match, Official off) {
		super(rfetcher);
		
		this.official = off;
		this.match = match;
	}
	
	public String raise() throws Exception {
		if (official == null) return "";
		String url = null;
		
		if (this.getResourceFetcher() != null) {
			
			OfficialArgumentBuilder obuilder = new OfficialArgumentBuilder();
			StringBuilder src = null;
			
			for (int i=0; i<this.getSize(); i++) {
				
				try {
					
					url = obuilder.buildArgument(official);
					ResourceFetchable fetcher = this.getFetcher(i);
					Dlogger.debug("    fetching official-player page ['" + url + "']..."); 
					src = fetcher.fetchResource(url);
					if (src != null) break;
					
				} catch (Exception ex) {
					
				}
			}
			
			if (src == null) {
				throw new Exception (" * Unable to load official-player " + official.getId() + " [" + official.getTitleName() + "]!");
			}
			
			Dlogger.debug("    backing-up fetched official player data to the repository with ID [" + this.official.getId() + "]...");
			new BackupFetchedPlayer(src).backup(new String[] {this.official.getId()});
			
			Dlogger.debug("    parsing official player page..."); 
			HTMLPlayerPageParser prParser = new HTMLPlayerPageParser(new DefaultPlayerDetailFilter());
			FullPlayer fp = prParser.parsePlayer(src);
			fp.setId(this.official.getId());
			fp.setDisplayName(this.official.getTitleName());
			if (fp.getShortName() == null) fp.setShortName(this.official.getShortName());
			
			String query = new PlayerQueryGenerator(match, fp).generateQuery();
			Dlogger.debug(query);
			
			if (query != null && query.length() > 0) {
				Dlogger.debug("       Adding new official to the database...");
				String[] qs = query.split("\n");
				for (int j=0;j<qs.length;j++) {
					qs[j] = qs[j].trim();
					if (qs[j].trim().length() == 0) continue;
					
					DBThread dbt = new DBThread(qs[j].trim()); 
					dbt.run(); dbt.close();
					
				}
				Dlogger.debug("       success.");
			}
			
		} else {
			
			Dlogger.warn("    unable to locate resource fetcher for official player!");
			
		}
		
		return "";
	}
	
	public void setMatch(CricketMatch match) {
		this.match = match;
	}
	
	public CricketMatch getMatch() {
		return match;
	}

	public void setOfficial(Official official) {
		this.official = official;
	}
	
	public Official getOfficial() {
		return official;
	}
	
}
